export function chooseImage() {
  return new Promise((resolve, reject) => {
    uni.getSetting({
      async success(res) {
        if (res.authSetting['scope.writePhotosAlbum']) {
          // 已授权，直接保存图片
          // 这里写保存图片代码.......
          resolve(await uploadImg())
        } else if (res.authSetting['scope.writePhotosAlbum'] === false) {
          // 用户已拒绝授权，提示用户授权
          uni.showModal({
            title: '提示',
            content: '您未授权保存图片到相册，是否前往设置页面进行授权？',
            success: function (res) {
              if (res.confirm) {
                uni.openSetting({
                  success: async function (res) {
                    if (res.authSetting['scope.writePhotosAlbum']) {
                      resolve(await uploadImg())
                    }
                  }
                })
              } else if (res.cancel) {
                // eslint-disable-next-line prefer-promise-reject-errors
                reject()
                uni.showToast({
                  title: '您取消了授权',
                  icon: 'none',
                  duration: 2000
                })
              }
            }
          })
        } else {
          // 用户第一次调用，调用授权接口
          uni.authorize({
            scope: 'scope.writePhotosAlbum',
            async success() {
              // 这里写保存图片代码.......
              resolve(await uploadImg())
            },
            fail() {
              // eslint-disable-next-line prefer-promise-reject-errors
              reject()
              uni.showToast({
                title: '授权失败，请稍后重试',
                icon: 'none',
                duration: 2000
              })
            }
          })
        }
      }
    })
  })
}

// 封装的本地选择图片
async function uploadImg() {
  try {
    const res = await uni.chooseImage({
      count: 1
    })
    const url = 'http://120.24.43.109:8080/jeecg-boot//jx/oss/upload' // 上传地址
    const timestamp = new Date().getTime() // 文件名
     uni.uploadFile({
      url, // 上传地址
      filePath: res.tempFilePaths[0], // 上传文件的临时路径
      name: 'file',
      formData: {
        name: 'app/' + timestamp, // 文件名
        key: 'app/' + timestamp, // 文件名
        success_action_status: '200'
      },
      success: (e) => {
        // 上传成功的图片地址
        const httpurl = JSON.parse(e.data).message
      }
    })
   
    // userInfo.image = url + '/app/' + timestamp
  } catch (err) {
    console.log(err)
  }
}

// 获取状态栏高度
export async function getNavHeight() {
  const res = await uni.getSystemInfo()
  return res.statusBarHeight || 0
}
